package com.project.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.StockBean;
import com.project.mapper.IStockMapper;
import com.project.service.IStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class StockServiceImpl implements IStockService {
    @Autowired
    private IStockMapper stockMapper;

    @Override
    public StockBean findById(String id) {//通过商品编号，查找商品
        return stockMapper.selectById(id);
    }

    @Override
    public IPage<StockBean> findOutByItem(Integer pageNO, String name, String kind) {
        //   添加出库商品的  动态分页查询所有有销售价的商品
        //     * 通过输入的商品名称、类别动态条件、分页查询商品信息。
        QueryWrapper<StockBean> qw = new QueryWrapper<>();
        if (name != null && name.length() != 0) {
            qw.like("st_name", name);
        }
        if (kind != null && kind.length() != 0) {
            qw.like("st_kind", kind);
        }
        qw.ge("st_num",1);
        qw.ge("st_outPrice",1);
        return stockMapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public IPage<StockBean> findByItem(Integer pageNO, String name, String outPrice,
                                       String kind, Integer startNum, Integer endNum) {
        ///** 库存一览
        //     * 根据输入的条件动态的查询库存商品信息
        QueryWrapper<StockBean> qw = new QueryWrapper<>();
        if (name != null && name.length() != 0) {
            qw.like("st_name", name);
        }
        if (kind != null && kind.length() != 0) {
            qw.like("st_kind", kind);
        }
        if (outPrice.equals("未定销售价") ) {
            qw.eq("st_outPrice",0);
        }
        if (startNum!=null){
            qw.ge("st_num",startNum);
        }
        if (endNum!=null){
            qw.le("st_num",endNum);
        }
        return stockMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public void updateInfo(String id, Integer newOutPrice) {
        //通过库存商品id编号修改销售价格
        StockBean stock=stockMapper.selectById(id);
        stock.setOutPrice(newOutPrice);
        stockMapper.updateById(stock);
    }
}
